#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project: DataStructure-Python
@File: Single-Circular-Linked-List.py
@Author: Dragon Geek
@Date: 2021/4/19 10:15
@Description: Bubble Sort Algorithm
"""


class Node(object):
    # 单向循环链表的节点
    def __init__(self, elem):
        # elem存放数据元素
        self.elem = elem
        # next存放节点标识
        self.next = None


class SingleCircularLinkedList(object):
    # 单向循环链表
    def __init__(self, node=None):
        self.__head = node
        if node:
            node.next = node

    # 链表是否为空
    def is_empty(self):
        return self.__head is None

    # 返回链表的长度
    def length(self):
        # cur游标，用来移动遍历节点
        if self.__head:
            cur = self.__head
            # count记录数量
            count = 1
            while cur.next is not self.__head:
                count += 1
                cur = cur.next
        else:
            count = 0
        return count

    # 遍历整个列表
    def travel(self):
        # cur游标，用来移动遍历节点
        if self.is_empty():
            print("Empty List!")
        else:
            cur = self.__head
            while cur.next is not self.__head:
                print(cur.elem, end=" ")
                cur = cur.next
            print(cur.elem)

    # 链表头部增加元素
    def add(self, item):
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = self.__head
        else:
            cur = self.__head
            while cur.next is not self.__head:
                cur = cur.next
            node.next = self.__head
            self.__head = node
            cur.next = self.__head

    # 链表尾部增加元素
    def append(self, item):
        node = Node(item)
        if self.is_empty():
            self.__head = node
            node.next = self.__head
        else:
            cur = self.__head
            while cur.next is not self.__head:
                cur = cur.next
            cur.next = node
            node.next = self.__head

    """
    指定位置添加元素
    :param position: 从0开始
    """

    def insert(self, position, item):
        if position <= 0:
            self.add(item)
        elif position >= (self.length() - 1):
            self.append(item)
        else:
            node = Node(item)
            pre = self.__head
            count = 0
            while count < position:
                count += 1
                pre = pre.next
            node.next = pre.next
            pre.next = node

    # 删除节点
    def remove(self, item):
        pre = self.__head
        cur = self.__head
        if self.is_empty():
            print("Empty List!")
        else:
            while cur.next is not self.__head:
                if cur.elem == item:
                    # 当被删除元素在头结点
                    if cur == self.__head:
                        # 定位到尾结点元素
                        end = self.__head
                        while end.next is not self.__head:
                            end = end.next
                        self.__head = cur.next
                        end.next = self.__head
                    else:
                        pre.next = cur.next
                    return
                else:
                    pre = cur
                    cur = cur.next
            # 当被删除元素在尾结点
            if cur.elem == item:
                if cur == self.__head:
                    self.__head = None
                else:
                    pre.next = self.__head

    # 查找节点是否存在
    def search(self, item):
        if self.is_empty():
            return False
        cur = self.__head
        while cur.next is not self.__head:
            if cur.elem == item:
                return True
            else:
                cur = cur.next
        if cur.elem == item:
            return True
        return False
    # The second way
    # def seSingleCircularLinkedList_01.search(1)arch(self, item):
    #     cur = self.__head
    #     if cur:
    #         while cur.next is not self.__head:
    #             if cur.elem == item:
    #                 return True
    #             else:
    #                 cur = cur.next
    #         if cur.elem == item:
    #             return True
    #     return False


if __name__ == "__main__":
    SingleCircularLinkedList_01 = SingleCircularLinkedList()
    print(SingleCircularLinkedList_01.is_empty())
    print(SingleCircularLinkedList_01.length())
    SingleCircularLinkedList_01.travel()
    SingleCircularLinkedList_01.remove(1)
    print(SingleCircularLinkedList_01.search(1))
    SingleCircularLinkedList_01.add(1)
    SingleCircularLinkedList_01.add(2)
    SingleCircularLinkedList_01.add(3)
    SingleCircularLinkedList_01.add(4)
    SingleCircularLinkedList_01.add(5)
    SingleCircularLinkedList_01.add(6)
    SingleCircularLinkedList_01.add(7)
    SingleCircularLinkedList_01.travel()
    SingleCircularLinkedList_01.append(1)
    SingleCircularLinkedList_01.append(2)
    SingleCircularLinkedList_01.append(3)
    SingleCircularLinkedList_01.append(4)
    SingleCircularLinkedList_01.append(5)
    SingleCircularLinkedList_01.append(6)
    SingleCircularLinkedList_01.append(7)
    SingleCircularLinkedList_01.travel()
    print(SingleCircularLinkedList_01.search(100))
    print(SingleCircularLinkedList_01.search(1))
    SingleCircularLinkedList_01.append(100)
    SingleCircularLinkedList_01.add(200)
    SingleCircularLinkedList_01.travel()
    SingleCircularLinkedList_01.remove(100)
    SingleCircularLinkedList_01.travel()
    SingleCircularLinkedList_01.remove(200)
    SingleCircularLinkedList_01.travel()
    SingleCircularLinkedList_01.remove(1)
    SingleCircularLinkedList_01.remove(7)
    SingleCircularLinkedList_01.travel()

